import os
from django.template import Library, Node, NodeList, resolve_variable
from apps.stat.models.stat import Stat
from apps.core.models.element import Element
import urllib2
register = Library()

@register.inclusion_tag('stats_fe_list.html')
def getStatsList(user):
    sList = Element.objects.getElements(user, "stat:Stat")
    for s in sList:
        try:
            u = urllib2.urlopen(s.stat_set.all()[0].url)
            s.val = u.read()
            try:
                from django.utils.safestring import mark_safe
                s.val = mark_safe(s.val)
            except:
                pass
            u.close()
        except:
            s.val = 0
    return {"sList":sList}